package com.lori.netshop.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.pojo.Iteminfo;
import com.lori.netshop.pojo.SolrResult;
import com.lori.netshop.pojo.TbItemDesc;
import com.lori.netshop.pojo.TbItemParamItem;
import com.lori.netshop.service.ItemService;
import com.lori.netshop.util.HttpClientUtil;
import com.lori.netshop.util.NetshopResult;
import com.lori.netshop.util.StringUtil;
@Service
public class ItemServiceimp implements ItemService{
	@Value("${BASEURI}")
	private String BASEURI;
	@Value("${BASE_SEARCH_URL}")
	private String BASE_SEARCH_URL;
	@Value("${ITEM_SEARCH_URL}")
	private String ITEM_SEARCH_URL;
	@Value("${BASE_ITEM_URL}")
	private String BASE_ITEM_URL;
	@Value("${DESC_ITEM_URL}")
	private String DESC_ITEM_URL;
	@Value("${PARAM_ITEM_URL}")
	private String PARAM_ITEM_URL;
	
	@Override
	public SolrResult findItem(String queryString, Integer page) {
		try {
			//使用httpclient向服务端发送请求，获得数据
			String data = HttpClientUtil.doGet(BASE_SEARCH_URL+ITEM_SEARCH_URL+"?q="+queryString+"&page="+page);
			//将data转成java数据类型
			NetshopResult result = NetshopResult.formatToPojo(data, SolrResult.class);
			return (SolrResult) result.getData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	/**
	 * 根据商品id获得商品基本信息
	 */
	@Override
	public Iteminfo findIteminfo(long id) {
		try {
			//使用httpclient访问rest获得数据
			String json = HttpClientUtil.doGet(BASEURI+BASE_ITEM_URL+id);
			//将数据转成java数据
			if(!StringUtil.isEmpty(json)){
				NetshopResult result = NetshopResult.formatToPojo(json, Iteminfo.class);
				if(result != null && result.getStatus() == 200){
					return (Iteminfo) result.getData();
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 根据商品id获得商品详细信息
	 * 
	 */
	
	public String findItemdesc(long itemid){
		try {
			//使用httpclient访问服务端rest获得数据
			String json = HttpClientUtil.doGet(BASEURI+DESC_ITEM_URL+itemid);
			if(!StringUtil.isEmpty(json)){
				//将json转成java数据
				NetshopResult result = NetshopResult.formatToPojo(json, TbItemDesc.class);
				if(result != null){
					TbItemDesc data = (TbItemDesc) result.getData();
					return data.getItemDesc();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 *  根据商品id获得商品规格信息
	 */
	@Override
	public String findItemparam(long itemId) {
		try {
			//使用httpclient访问服务端rest获得数据
			String json = HttpClientUtil.doGet(BASEURI+PARAM_ITEM_URL+itemId);
			if(!StringUtil.isEmpty(json)){
				//将json转成java数据
				NetshopResult result = NetshopResult.formatToPojo(json, TbItemParamItem.class);
				if(result != null){
					TbItemParamItem data = (TbItemParamItem) result.getData();
					//将数据转成map格式
					ObjectMapper mapper = new ObjectMapper();
					JavaType javatype = mapper.getTypeFactory().constructParametricType(ArrayList.class, HashMap.class);
					List<Map> parse = mapper.readValue(data.getParamData(), javatype);
					//转成html字符串
					StringBuilder sb = new StringBuilder();
					sb.append("<div class=\"Ptable\" style=\"font-size:14px;color:#666;\">\n");
					for(Map map:parse){
						sb.append("    <div class=\"Ptable-item\" style=\"width:800px;border-bottom:1px solid #a1a1a1;overflow:auto;\">\n" );
						sb.append("        <h3 style=\"width:200px;float:left;font-size:14px;\">"+map.get("group")+"</h3>\n" );
						sb.append("        <dl style=\"width:550px;float:left;\">\n" );
						List<Map> list2 = (List<Map>) map.get("params");
						for(Map map2:list2){
							sb.append("            <dt style=\"width:100px;float:left;margin-bottom:20px;\">"+map2.get("k")+"</dt><dd style=\"width:400px;float:left;margin-bottom:20px;\">"+map2.get("v")+"</dd>\n" );
						}
						sb.append("        </dl>\n" );
						sb.append("    </div>\n" );
					}
					sb.append("</div>");
					return sb.toString();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	
	
	
}
